﻿using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using O1.Kernel.Storage.Transactions;

namespace Tests.O1.Kernel.Storage.Transactions
{
    [TestClass]
    public class MultiVersionTests
    {
        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void MultiVersionNegativeTxIsConsideredCommitted()
        {
            (-1234L).State().Should().Be(MultiVersionState.Committed);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void MultiVersionPositiveTxIsConsideredUnCommitted()
        {
            (1234L).State().Should().Be(MultiVersionState.Pending);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void MultiVersionSetPendingSetsValueToPositiveTx()
        {
            // The behavior is to offset from the sign bit, so as a negative
            // approaches 0, most of the bits are on. +1 to accommodate 0 being
            // sensibly handled as all bits off.
            (-1234L).AsPending().Should().Be(long.MaxValue - 1234L + 1);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void MultiVersionSetPendingLeavesPendingValueUnchanged()
        {
            (1234L).AsPending().Should().Be(1234L);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void MultiVersionSetCommittedSetsValueToNegativeTx()
        {
            (1234L).AsCommitted().Should().Be(long.MinValue + 1234L);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void MultiVersionSetCommittedLeavesCommittedValueUnchanged()
        {
            (-1234L).AsCommitted().Should().Be(-1234L);
        }
    }
}